package com.hyf.test;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.util.ResourceBundle;


public class Mytest {
   @Test
    public void u1(){
       ClassPathXmlApplicationContext applicationContext=
               new ClassPathXmlApplicationContext("spring.xml");
       Object  u1=applicationContext.getBean("u1");
       System.out.println(u1);
       applicationContext.close();
   }
    @Test
    public void c3p0() throws Exception {
       //创建数据源
       ComboPooledDataSource c3p0 = new ComboPooledDataSource();
       //设置数据库连接参数
       c3p0.setDriverClass("com.mysql.jdbc.Driver");
       c3p0.setJdbcUrl("jdbc:mysql://localhost:3306/check");
       c3p0.setUser("root");
       c3p0.setPassword("123456");
       //获得连接对象
       Connection connection = c3p0.getConnection();
       System.out.println(connection);
       connection.close();
       c3p0.close();
    }
    @Test
    public void druid() throws Exception {
       //创建数据源
       DruidDataSource druid = new DruidDataSource();
       //设置数据库连接参数
       druid.setDriverClassName("com.mysql.jdbc.Driver");
       druid.setUrl("jdbc:mysql:///check");
       druid.setUsername("root");
       druid.setPassword("123456");
       //获得连接对象
       Connection connection = druid.getConnection();
       System.out.println(connection);
       connection.close();
       druid.close();
    }
    @Test
    public void c3p0test() throws Exception {
       //加载类路径下的jdbc.properties
       ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
       ComboPooledDataSource c3p0 = new ComboPooledDataSource();
       c3p0.setDriverClass(jdbc.getString("jdbc.driver"));
       c3p0.setJdbcUrl(jdbc.getString("jdbc.url"));
       c3p0.setUser(jdbc.getString("jdbc.username"));
       c3p0.setPassword(jdbc.getString("jdbc.password"));
       Connection connection = c3p0.getConnection();
       System.out.println(connection);
       connection.close();
       c3p0.close();
    }
    @Test
    public void druidtest() throws Exception {
       //加载类路径下的jdbc.properties
       ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
       //无参构造创建数据源
       DruidDataSource druid = new DruidDataSource();
       druid.setDriverClassName(jdbc.getString("jdbc.driver"));
       druid.setUrl(jdbc.getString("jdbc.url"));
       druid.setUsername(jdbc.getString("jdbc.username"));
       druid.setPassword(jdbc.getString("jdbc.password"));
       //获得连接对象
       Connection connection = druid.getConnection();
       System.out.println(connection);
       //关闭连接池
       connection.close();
       druid.close();
    }
    @Test
   public void druidSpring()throws Exception{
       ClassPathXmlApplicationContext applicationContext=
               new ClassPathXmlApplicationContext("spring.xml");
       DruidDataSource druid =( DruidDataSource) applicationContext.getBean("druid");
       System.out.println(druid.getConnection());
       applicationContext.close();
    }
    @Test
    public void c3p0Spring()throws Exception{
       ClassPathXmlApplicationContext applicationContext=
               new ClassPathXmlApplicationContext("spring.xml");
       ComboPooledDataSource c3p0 =( ComboPooledDataSource) applicationContext.getBean("c3p0");
       System.out.println(c3p0.getConnection());
       applicationContext.close();
    }

}
